/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package LwuitUI;

/**
 *
 * @author anup.s
 */
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.List;
import com.sun.lwuit.list.ListCellRenderer;
import com.sun.lwuit.Image;
import com.sun.lwuit.layouts.*;
import com.sun.lwuit.plaf.*;
import com.sun.lwuit.geom.Dimension;


import java.util.Hashtable;
import java.util.Enumeration;

import mujmail.Settings;
import mujmail.account.MailAccount;
import mujmail.Lang;
import mujmail.util.Functions;

public class AccountsRenderer extends Container implements ListCellRenderer {

    private Image imPrimaryAcc,  imActiveAcc,  imInActiveAcc;
    private com.sun.lwuit.Label icon = new com.sun.lwuit.Label("");
    private com.sun.lwuit.Label text = new com.sun.lwuit.Label("");
    private com.sun.lwuit.Label status = new com.sun.lwuit.Label("");
    private static final boolean DEBUG = false;

    public AccountsRenderer() {
        imPrimaryAcc = Functions.getIcon("acc_primary.png");
        imActiveAcc = Functions.getIcon("acc_active.png");
        imInActiveAcc = Functions.getIcon("acc_inactive.png");
        setLayout(lcduilwuit.Display.boxLayout_Y);
        
        Style  a = text.getStyle();
        a.setPadding(Component.BOTTOM, 0);
        a.setMargin(Component.BOTTOM, 0);
        text.setStyle(a);

        Style  b = status.getStyle();
        //b.setPadding(Component.BOTTOM, 0);
       // b.setMargin(Component.BOTTOM, 0);
        b.setPadding(Component.TOP, 0);
        b.setMargin(Component.TOP, 0);
        status.setStyle(b);

        Style  c = icon.getStyle();
       // c.setPadding(Component.BOTTOM, 0);
       // c.setMargin(Component.BOTTOM, 0);
        c.setPadding(Component.TOP, 0);
        c.setMargin(Component.TOP, 0);
        icon.setStyle(c);

        text.getStyle().setBgTransparency(0);
        addComponent(text);
        Container cnt = new Container(new BorderLayout());
        
        //name.getStyle().setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM));
        status.getStyle().setBgTransparency(0);
        icon.getStyle().setBgTransparency(0);
        cnt.addComponent(BorderLayout.WEST,icon);
        cnt.addComponent(BorderLayout.CENTER,status);
        if (DEBUG) System.out.println("Container Heighrt="+cnt.getHeight()+" Sum=    "+(text.getHeight()+status.getHeight()));
        //cnt.setPreferredSize(new Dimension(text.getWidth(),text.getHeight()+status.getHeight()));//+status.getHeight()-status.getHeight()/2);
        //cnt.setPreferredSize(new Dimension(cnt.getPreferredSize().getWidth(),text.getHeight()+status.getHeight()));
        Style s1 = cnt.getStyle();
        s1.setPadding(Component.BOTTOM, 0);
        s1.setMargin(Component.BOTTOM, 0);
        
       // s.setBorder(Border.createLineBorder(1));
        cnt.setStyle(s1);
        addComponent( cnt);
        Style s = getStyle();
      //  s.setPadding(Component.BOTTOM, 0);
        //s.setMargin(Component.BOTTOM, 0);
        s.setBorder(Border.createLineBorder(1));
        setStyle(s);
       // focus.getStyle().setBgTransparency(100);
     //   focus.getStyle().setBgColor(0x00FF00);
    }

    public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {

        MailAccount account = (MailAccount) value;
        String info = account.isActive() ? Lang.get(Lang.ACTIVE) : Lang.get(Lang.INACTIVE);
        Image img = imInActiveAcc;
        //an account must be active in order to be primary,
        //so if it has icon imPrimary, it means its active and also primary
        if (Settings.primaryEmail.equals(account.getEmail())) {
            info += " | " + Lang.get(Lang.AS_PRIMARY);
            img = imPrimaryAcc;
        } else if (account.isActive()) //its active only
        {
            img = imActiveAcc;
        }
        icon.setIcon(img);
        status.setText(info);
        text.setText(account.getEmail());
        return this;

    }

    public Component getListFocusComponent(List list) {
        return Menu.focus;
    }
}
